package com.tzu.ft.base;

import com.tzu.ssl.KeyInfo;
import com.tzu.ssl.SSLTools;
import com.tzu.utils.PropertyConfig;
import com.tzu.utils.StringTools;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * 配置文件
 */
public class FTConfig {

    private static final String CONFIG_DIR_NAME = "FT_CONFIG";

    private static final String CONFIG_SSL_DIR_NAME = "SSL";

    private static final String CONFIG_FILE_NAME = "ftconfig.properties";

    public static final String FTKEY_default_save_path = "default_save_path";
    public static final String FTKEY_task_thread_num = "task_thread_num";
    public static final String FTKEY_ssl_keypass = "ssl_keypass";
    public static final String FTKEY_stream_buffer_size = "stream_buffer_size";

    public static final String VAL_SSL_DEFAULT_PASSWD="Tzu*123_";

    /**
     * 最大任务并行数
     */
    public static final int DEFAULT_MAX_TASK_THREAD_NUM = 200;

    private static String configPath = "";

    // 缓存保持
    private static PropertyConfig propertyConfig;

    static {
        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载配置
     */
    private static void init() throws IOException {
        String basepath = FTTools.getClassPath();
        // 创建文件夹
        File dirpath = Paths.get(basepath, CONFIG_DIR_NAME).toFile();
        if (!dirpath.exists() || !dirpath.isDirectory()) {
            dirpath.mkdirs();
        }
        configPath = dirpath.getAbsolutePath();
        // 创建配置文件
        File configfile = Paths.get(configPath, CONFIG_FILE_NAME).toFile();
        if (!configfile.exists()) {
            configfile.createNewFile();
        }
        // 加载配置
        propertyConfig = new PropertyConfig(configfile.getAbsolutePath());
        System.out.println("缓存路径：" + configPath);
    }

    /**
     * 获取配置路径
     */
    public static String getConfigPath() {
        return configPath;
    }

    /**
     * 获取默认保持路径
     */
    public static String getDefaultSavePath() {
        return propertyConfig.getConfig(FTKEY_default_save_path);
    }

    /**
     * 设置默认保持路径
     */
    public static void setDefaultSavePath(String path) {
        propertyConfig.addConfig(FTKEY_default_save_path, path, true);
    }

    /**
     * 获取允许线程
     */
    public static int getTaskThreadNum() {
        String at = propertyConfig.getConfig(FTKEY_task_thread_num);
        if (null == at || at.equals("")) {
            return 30;
        } else {
            return Integer.parseInt(at);
        }
    }

    /**
     * 设置允许线程
     */
    public static void setTaskThreadNum(int threads) {
        propertyConfig.addConfig(FTKEY_task_thread_num, threads + "", true);
    }

    /**
     * 获取ssl文件夹路径
     */
    public static String getSslPath() {
        String basepath = FTTools.getClassPath();
        // 创建文件夹
        File dirpath = Paths.get(basepath, CONFIG_DIR_NAME, CONFIG_SSL_DIR_NAME).toFile();
        if (!dirpath.exists() || !dirpath.isDirectory()) {
            dirpath.mkdirs();
        }
        return dirpath.getAbsolutePath();
    }

    /**
     * 获取ssl私钥
     */
    public static String getSslTrustStorePath() {
        String basepath = getSslPath();
        File keyfile = Paths.get(basepath, SSLTools.FILE_NAME_TRUSTSTORE).toFile();
        return keyfile.getAbsolutePath();

    }


    /**
     * 获取ssl公钥
     */
    public static String getSslKeyStorePath() {
        String basepath = getSslPath();
        File keyfile = Paths.get(basepath, SSLTools.FILE_NAME_KEYSTORE).toFile();
        return keyfile.getAbsolutePath();
    }


    /**
     * 证书默认密码
     */
    public static String getSslKeyPass() {
        String keypass = propertyConfig.getConfig(FTKEY_ssl_keypass);
        if (StringTools.isBlank(keypass)) {
            return VAL_SSL_DEFAULT_PASSWD;
        } else {
            return keypass;
        }
    }

    /**
     * 证书默认密码
     */
    public static void setSslKeyPass(String keypass) {
        propertyConfig.addConfig(FTKEY_ssl_keypass, keypass, true);
    }

    /**
     * 生成证书信息
     */
    public static KeyInfo getKeyInfo() {
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.setAlias("ftkey");
        keyInfo.setKeypass(getSslKeyPass());
        keyInfo.setKeyalg(SSLTools.SignAlgo.RSA);
        keyInfo.setSignAlgo(SSLTools.SignAlgo.SHA1_RSA);
        keyInfo.setKeysize(SSLTools.SignAlgo.RSA_DEF_LENGTH);
        keyInfo.setKeyStoreType(SSLTools.KeyStoreType.JKS);

        keyInfo.setKeyStorePath(getSslKeyStorePath());
        keyInfo.setTrustStorePath(getSslTrustStorePath());

        return keyInfo;
    }

    /**
     * 获取流的缓冲大小，单位：KiB
     */
    public static int getStreamBufferSize() {
        String buffer = propertyConfig.getConfig(FTKEY_stream_buffer_size);
        int buffersize = 128;
        if (StringTools.isNotBlank(buffer)) {
            buffersize = Integer.parseInt(buffer);
        }
        // 默认值128kb
        return buffersize;
    }

    /**
     * 获取流的缓冲大小，单位：KiB
     */
    public static void setStreamBufferSize(int size) {
        propertyConfig.addConfig(FTKEY_stream_buffer_size, size + "", true);
    }

    // ==========================================
    public static void main(String[] args) throws IOException {
        init();
        setDefaultSavePath("D:\\TEST\\ft");
    }

}
